package com.hxzy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/10/12 14:21
 * @Version 1.0
 */
@RestController
public class LoadBalancerRestTemplateController {

    /**
     * 自动注入对象
     */
    @Autowired
    private RestTemplate restTemplate;


    @GetMapping(value = "/lb/rest/order")
    public String callOrderService(){
        Map<String,Object> map=new HashMap<>();
        map.put("productId",1000);
        map.put("num",2);
        map.put("userId",2);
        map.put("shopId",100);

        //1、调用远程服务，它使用json格式接收数据
        HttpHeaders requestHeaders = new HttpHeaders();
        // 重点是配置请求头内容类型为："application/json"
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);

        //2、定义远程地址 http://192.168.21.100:19000
        //定义就要使用http://服务名/请求地址
        String url="http://order-server/order/create";

        //3、参数和请求参数型包装起来
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<Map<String, Object>>(map, requestHeaders);

        //4、使用restTemplate发送post请求并得到响应结果

        String data= restTemplate.postForObject(url, requestEntity, String.class);

        return data;
    }
}
